# Set of identifiers for inline display backends
INLINE_DISPLAY_BACKENDS = {
    "module://ipykernel.pylab.backend_inline",  # Classic Jupyter inline backend
    "module://matplotlib_inline.backend_inline",  # New Jupyter inline backend
    "nbAgg",  # Jupyter notebook interactive backend
}


def auto_close_inline_figures(figure):
    """Automatically close matplotlib figures when using inline display backends
    to prevent duplicate rendering.
    
    In inline display environments, matplotlib automatically captures and displays
    figures, while the returned figure object also gets displayed, causing duplicates.
    This function detects inline backends and closes the initial figure to ensure
    single display instance.
    
    Args:
        figure: matplotlib Figure object to close if inline backend is detected
    """
    import matplotlib
    
    # Check if current backend is an inline display type
    if matplotlib.get_backend() in INLINE_DISPLAY_BACKENDS:
        import matplotlib.pyplot as plt
        plt.close(figure)